//============================================================================
// Copyright (C) 2010 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include "DtwGame.h"


//------------------------------------------------------------------------------
//! if tutorial text has been set then draw it
void DtwStateMenu::DrawTutorialText( void )
{
	// draw timed text
	if( m_oTutorialTextInfo.m_strText.size() > 0 ) 
	{
		// Get tutorial text alpha
		irr::video::SColor oTextColor(255, 255, 255, 255);
		irr::video::SColor oBoxColor(160, 255, 255, 255);
		U32 u32TimeLeft = m_oTutorialTextInfo.m_u32DeleteTime - m_u32Timer;
		if( u32TimeLeft < 2000 ) 
		{
			F32 f32TimeSeconds = (F32)u32TimeLeft;
			f32TimeSeconds = f32TimeSeconds / 1000.0f;

			// if less than 2 seconds left then fade
			oTextColor.setAlpha((irr::u32)(255 * f32TimeSeconds / 2.0));
			oBoxColor.setAlpha((irr::u32)(160 * f32TimeSeconds / 2.0));
		}

		// draw text box
		int iWidth	= 370; 
		int iHeight = 115;
		DrawTextBox( m_poDriver->getScreenSize().Width / 2, m_poDriver->getScreenSize().Height - iHeight / 2 - 30, iWidth, iHeight, oBoxColor );
		// draw the text
		RenderText(	m_oTutorialTextInfo.m_strText.c_str(), 
					m_poDriver->getScreenSize().Width / 2, 
					m_poDriver->getScreenSize().Height - iHeight - 13, 
					eGuiAlignCenter, 
					eGuiFontMedium, 
					oTextColor);
	}
}

//------------------------------------------------------------------------------
//! get rectagle in centered in center of screen
irr::core::rect<irr::s32> DtwStateMenu::GetCenteredRect( int iPosX, int iPosY, int iWidth, int iHeight)
{
	return irr::core::rect<irr::s32>(iPosX - (iWidth >> 1), iPosY - (iHeight >> 1), iPosX + (iWidth >> 1), iPosY + (iHeight >> 1));
}

//------------------------------------------------------------------------------
//! make irrlicht rect from screen positions
irr::core::rect<irr::s32> DtwStateMenu::GetRect( int iPosX, int iPosY, int iWidth, int iHeight)
{
	return irr::core::rect<irr::s32>( iPosX, iPosY, iPosX + iWidth, iPosY + iHeight );
}
//------------------------------------------------------------------------------
//! change gui skin
void DtwStateMenu::ChangeSkin( EGuiSkinType eSkinType ) 
{
	m_poGuiSkin->setColor( irr::gui::EGDC_BUTTON_TEXT, irr::video::SColor(255, 255, 255, 255));
	m_poGuiSkin->setColor( irr::gui::EGDC_WINDOW, irr::video::SColor(255, 0, 0, 20));	
	m_poGuiSkin->setColor( irr::gui::EGDC_WINDOW_SYMBOL, irr::video::SColor(255, 255, 255, 255));
	
	switch( eSkinType ) 
	{
	case eGuiSkinTypeGame:
		//m_poGuiSkin->setColor( irr::gui::EGDC_3D_FACE, irr::video::SColor(255, 0, 0, 0));
		//m_poGuiSkin->setColor( irr::gui::EGDC_3D_HIGH_LIGHT, irr::video::SColor(255, 0, 0, 0));
		//m_poGuiSkin->setColor( irr::gui::EGDC_3D_SHADOW, irr::video::SColor(255, 0, 0, 0));
		//m_poGuiSkin->setColor( irr::gui::EGDC_3D_DARK_SHADOW, irr::video::SColor(255, 0, 0, 0));
		//break;
	case eGuiSkinTypeMenu:
		// use this color so button image transparent areas are actually transparent.. if use
		// irr::video::SColor(255, 0, 0, 0) then tranparent parts will actually be black
		m_poGuiSkin->setColor( irr::gui::EGDC_3D_FACE, irr::video::SColor(0, 0, 0, 0));
		m_poGuiSkin->setColor( irr::gui::EGDC_3D_HIGH_LIGHT, irr::video::SColor(0, 0, 0, 0));
		m_poGuiSkin->setColor( irr::gui::EGDC_3D_SHADOW, irr::video::SColor(0, 0, 0, 0));
		m_poGuiSkin->setColor( irr::gui::EGDC_3D_DARK_SHADOW, irr::video::SColor(0, 0, 0, 0));
		break;
	case eGuiSkinTypeNeedSlider:
		m_poGuiSkin->setColor( irr::gui::EGDC_3D_FACE,  irr::video::SColor(101,210,210,210));
		m_poGuiSkin->setColor( irr::gui::EGDC_3D_HIGH_LIGHT, irr::video::SColor(101,255,255,255));
		m_poGuiSkin->setColor( irr::gui::EGDC_3D_SHADOW, irr::video::SColor(101,130,130,130));
		m_poGuiSkin->setColor( irr::gui::EGDC_3D_DARK_SHADOW, irr::video::SColor(101,50,50,50));
		break;
	default:
		break;
	}
}


//------------------------------------------------------------------------------
//! convert time to string to display
void DtwStateMenu::ConvertTimeToString( U32 u32TimeMs, char * pRetBuf )
{
	U32 u32MilliSec = u32TimeMs % 1000;
	u32TimeMs = u32TimeMs / 1000;
	U32 u32Minutes = u32TimeMs / 60;
	U32 u32Seconds = u32TimeMs % 60;
	sprintf( pRetBuf, "%.2d:%.2d:%.3d", u32Minutes, u32Seconds, u32MilliSec );
}

//------------------------------------------------------------------------------
//! fade screen to black
void DtwStateMenu::FadeScreen( F32 f32FadeAmount ) 
{
	m_poDriver->draw2DImage(m_apoGuiImages[ eGuiImageBlack ], 
							irr::core::position2di(0, 0), 
							irr::core::rect<irr::s32>(0, 0, m_poDriver->getScreenSize().Width, m_poDriver->getScreenSize().Height), 
							0, 
							irr::video::SColor((irr::u32)(f32FadeAmount * 255), 255, 255, 255), 
							true	);
}

//------------------------------------------------------------------------------
//! render text
void DtwStateMenu::RenderText(	const char * pText,		
								int iPosX, 
								int iPosY, 
								EGuiAlignType eAlignType, 
								EGuiFontSize eFontType, 
								const irr::video::SColor& oColor )
{
	// convert to wide string
	irr::core::stringw strWideText( pText );
	// get dimensions of rendered texts
	irr::core::dimension2d<irr::u32> dim2dTextArea = m_apoFonts[ eFontType ]->getDimension( strWideText.c_str() );

	// position according to alignment
	switch( eAlignType ) 
	{
	case eGuiAlignLeft:
		break;
	case eGuiAlignCenter:
		iPosX -= dim2dTextArea.Width >> 1;
		break;
	case eGuiAlignRight:
		iPosX -= dim2dTextArea.Width;
		break;
	}

	// draw the text
	m_apoFonts[ eFontType ]->draw( strWideText.c_str(), 
						irr::core::rect<irr::s32>(iPosX, iPosY, iPosX + dim2dTextArea.Width, iPosY + dim2dTextArea.Height), 
						oColor );
}

//------------------------------------------------------------------------------
//! draw image centered around a position
void DtwStateMenu::DrawImage( EGuiImageType eImageType, int iPosX, int iPosY, int iWidth, int iHeight, const irr::video::SColor& oColor) 
{
	m_poDriver->draw2DImage(	m_apoGuiImages[ eImageType ], 
								irr::core::position2di( iPosX - ( iWidth >> 1 ), 
								iPosY - ( iHeight >> 1 )), 
								irr::core::rect<irr::s32>( 0, 0, iWidth, iHeight ), 
								0, 
								oColor, 
								true );
}

//------------------------------------------------------------------------------
//! draw a rounded text box
void DtwStateMenu::DrawTextBox(int iPosX, int iPosY, int iWidth, int iHeight, const irr::video::SColor& oColor ) 
{
	iPosX -= iWidth >> 1;
	iPosY -= iHeight >> 1;

	// draw the rounded corners
	m_poDriver->draw2DImage(	m_apoGuiImages[ eGuiImageTextBoxCorner ], 
								irr::core::position2di( iPosX, iPosY ), 
								irr::core::rect<irr::s32>(
											0, 
											0, 
											(irr::s32)(10 * m_f32ScaleWidth), 
											(irr::s32)(10 * m_f32ScaleHeight) ),
								0, 
								oColor, 
								true );
	m_poDriver->draw2DImage(	m_apoGuiImages[ eGuiImageTextBoxCorner ], 
								irr::core::position2di( iPosX + iWidth - 10, iPosY ), 
								irr::core::rect<irr::s32>(
											(irr::s32)(10 * m_f32ScaleWidth), 
											0, 
											(irr::s32)(20 * m_f32ScaleWidth), 
											(irr::s32)(10 * m_f32ScaleHeight) ),
								0, 
								oColor, 
								true );
	m_poDriver->draw2DImage(	m_apoGuiImages[ eGuiImageTextBoxCorner ], 
								irr::core::position2di( iPosX, iPosY + iHeight - (irr::s32)(10 * m_f32ScaleHeight) ), 
								irr::core::rect<irr::s32>(
											0, 
											(irr::s32)(10 * m_f32ScaleHeight), 
											(irr::s32)(10 * m_f32ScaleWidth), 
											(irr::s32)(20 * m_f32ScaleHeight) ),
								0, 
								oColor, 
								true );
	m_poDriver->draw2DImage(	m_apoGuiImages[ eGuiImageTextBoxCorner ], 
								irr::core::position2di( iPosX + iWidth - (irr::s32)(10 * m_f32ScaleWidth), iPosY + iHeight - (irr::s32)(10 * m_f32ScaleHeight) ), 
								irr::core::rect<irr::s32>(
											(irr::s32)(10 * m_f32ScaleWidth), 
											(irr::s32)(10 * m_f32ScaleHeight), 
											(irr::s32)(20 * m_f32ScaleWidth), 
											(irr::s32)(20 * m_f32ScaleHeight) ),
								0, 
								oColor, 
								true );
	// draw center background
	m_poDriver->draw2DImage(	m_apoGuiImages[ eGuiImageBlack ], 
								irr::core::position2di( iPosX + (irr::s32)(10 * m_f32ScaleWidth), iPosY + (irr::s32)(10 * m_f32ScaleHeight) ), 
								irr::core::rect<irr::s32>(
											0, 
											0, 
											iWidth - (irr::s32)(20 * m_f32ScaleWidth), 
											iHeight - (irr::s32)(20 * m_f32ScaleHeight) ),
								0, 
								oColor, 
								true );
	// draw edges
	m_poDriver->draw2DImage(	m_apoGuiImages[ eGuiImageTextBoxTopEdge ], 
								irr::core::position2di( iPosX + (irr::s32)(10 * m_f32ScaleWidth), iPosY ), 
								irr::core::rect<irr::s32>(
											0, 
											0, 
											iWidth - (irr::s32)(20 * m_f32ScaleWidth), 
											(irr::s32)(10 * m_f32ScaleHeight) ),
								0, 
								oColor, 
								true );
	m_poDriver->draw2DImage(	m_apoGuiImages[ eGuiImageTextBoxTopEdge ], 
								irr::core::position2di( iPosX + (irr::s32)(10 * m_f32ScaleWidth), iPosY + iHeight - (irr::s32)(10 * m_f32ScaleHeight) ), 
								irr::core::rect<irr::s32>(
											0, 
											(irr::s32)(6 * m_f32ScaleWidth),
											iWidth - (irr::s32)(20 * m_f32ScaleWidth), 
											(irr::s32)(16 * m_f32ScaleHeight)), 
								0, 
								oColor, 
								true );
	m_poDriver->draw2DImage(	m_apoGuiImages[ eGuiImageTextBoxLeftEdge ], 
								irr::core::position2di( iPosX , iPosY + (irr::s32)(10 * m_f32ScaleWidth) ), 
								irr::core::rect<irr::s32>(
											0, 
											0, 
											(irr::s32)(10 * m_f32ScaleWidth), 
											iHeight - (irr::s32)(20 * m_f32ScaleHeight) ), 
								0, 
								oColor, 
								true );
	m_poDriver->draw2DImage(	m_apoGuiImages[ eGuiImageTextBoxLeftEdge ], 
								irr::core::position2di( iPosX + iWidth - (irr::s32)(10 * m_f32ScaleWidth), iPosY + (irr::s32)(10 * m_f32ScaleHeight) ), 
								irr::core::rect<irr::s32>(
											(irr::s32)(6 * m_f32ScaleWidth), 
											0, 
											(irr::s32)(16 * m_f32ScaleWidth), 
											iHeight - (irr::s32)(20 * m_f32ScaleHeight) ), 
								0, 
								oColor, 
								true );
}
